/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.gateway.server.domain.property;

import java.time.Duration;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.cloud.context.config.annotation.RefreshScope;

import java.util.List;

/**
 * gateway配置相关属性.
 *
 * @author zengdegui
 * @since 2024/4/6
 */
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@RefreshScope
@ConfigurationProperties(prefix = "gateway")
public class GatewayProperty {

    /**
     * 控制并发缓冲区数量.
     */
    @Builder.Default
    private Integer limitRate = 16;

    /**
     * 数据缓冲区大小（限制单次 buffer 大小）.
     */
    @Builder.Default
    private Integer limitDataBufferSize = 512;

    /**
     * 远程接口重试次数.
     */
    @Builder.Default
    private Integer remoteRetryCount = 3;

    /**
     * 远程接口重试间隔（单位：秒）.
     */
    @Builder.Default
    private Duration remoteRetryPeriod = Duration.ofSeconds(1);

    /**
     * 路由配置.
     */
    @Builder.Default
    @NestedConfigurationProperty
    private RouteConfig route = new RouteConfig();

    /**
     * IP黑名单配置.
     */
    @Builder.Default
    @NestedConfigurationProperty
    private IpBlackListConfig ipBlackList = new IpBlackListConfig();

    /**
     * API白名单配置.
     */
    @Builder.Default
    @NestedConfigurationProperty
    private ApiWhiteListConfig apiWhiteList = new ApiWhiteListConfig();

    /**
     * 认证配置.
     */
    @Builder.Default
    @NestedConfigurationProperty
    private AuthConfig auth = new AuthConfig();

    /**
     * 日志配置.
     */
    @Builder.Default
    @NestedConfigurationProperty
    private LogConfig log = new LogConfig();

    /**
     * 路由相关属性.
     *
     * @author zengdegui
     * @since 2024/4/6
     */
    @Data
    @SuperBuilder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class RouteConfig {

        /**
         * 是否将路由信息写入Redis.
         */
        @Builder.Default
        private Boolean writeRedis = Boolean.FALSE;

        /**
         * 是否将路由信息写入DB.
         */
        @Builder.Default
        private Boolean writeDb = Boolean.FALSE;

        /**
         * 是否将路由信息写入Nacos.
         */
        @Builder.Default
        private Boolean writeNacos = Boolean.TRUE;
    }

    /**
     * IP黑名单相关属性.
     *
     * @author zengdegui
     * @since 2024/4/6
     */
    @Data
    @SuperBuilder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class IpBlackListConfig {

        /**
         * 是否启用.
         */
        @Builder.Default
        private Boolean enabled = Boolean.TRUE;
    }

    /**
     * API白名单相关属性.
     *
     * @author zengdegui
     * @since 2024/4/6
     */
    @Data
    @SuperBuilder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ApiWhiteListConfig {

        /**
         * 是否启用.
         */
        @Builder.Default
        private Boolean enabled = Boolean.TRUE;

        /**
         * 忽略的API.
         */
        private List<String> ignoredApi;
    }

    /**
     * 认证相关属性.
     *
     * @author zengdegui
     * @since 2024/4/6
     */
    @Data
    @SuperBuilder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class AuthConfig {

        /**
         * 是否启用.
         */
        @Builder.Default
        private Boolean enabled = Boolean.TRUE;

        /**
         * 是否启用API权限验证.
         */
        @Builder.Default
        private Boolean enabledApiPermission = Boolean.TRUE;

        /**
         * 忽略API鉴权的API.
         */
        private List<String> ignoredApi;

        /**
         * 通用都有权限的API.
         */
        private List<String> generalApi;
    }

    /**
     * 日志相关属性.
     *
     * @author zengdegui
     * @since 2024/4/6
     */
    @Data
    @SuperBuilder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class LogConfig {

        /**
         * 是否启用.
         */
        @Builder.Default
        private Boolean enabled = Boolean.FALSE;

        /**
         * 忽略记录日志的API.
         */
        private List<String> ignoredApi;

        /**
         * 是否启用获取请求参数.
         */
        @Builder.Default
        private Boolean enabledRequestParam = Boolean.TRUE;

        /**
         * 是否启用获取请求体.
         */
        @Builder.Default
        private Boolean enabledRequestBody = Boolean.TRUE;

        /**
         * 请求体限制大小，超过部分丢弃，单位：KB.
         */
        @Builder.Default
        private Integer limitRequestBody = 10;

        /**
         * 是否启用返回响应结果.
         */
        @Builder.Default
        private Boolean enabledResponseBody = Boolean.FALSE;

        /**
         * 响应结果限制大小，超过部分丢弃，单位：KB.
         */
        @Builder.Default
        private Integer limitResponseBody = 512;
    }
}
